<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html>
<head>
    <title>新增书籍</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- 引入 Bootstrap -->
    <link  href="${pageContext.request.contextPath}css/css.css" rel="stylesheet" type="text/css">
    <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
    <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<style>
    img{
        width: 150px;
        height: 150px;
    }
</style>
<div class="container">

    <div class="row clearfix">
        <div class="col-md-12 column">
            <div class="page-header">
                <h1>
                    <small>新增书籍</small>
                </h1>
            </div>
        </div>
    </div>
    <form action="${pageContext.request.contextPath}/product/addBook" method="post" >
        书籍编号：<input type="text" name="pid"><br><br><br>
        书籍名字：<input type="text" name="pname"><br><br><br>
        书籍价格：<input type="text" name="price"><br><br>
        <%--书籍图片：<input type="text" name="photo"><br><br><br>--%>

        <div class="submit">
            <a href="javascript:void(0)" id="selectPhoto" onclick="uploadPhoto()">选择图片</a>

            <input type="file" id="photoFile" style="display: none;" onchange="upload()">

            <img id="preview_photo" src="" width="100px" height="100px" hidden>

            <input type="text" name="photo" id="productImg" hidden>
        </div>


        书籍库存：<input type="text" name="inventory"><br><br><br>
        书籍内容：<input type="text" name="detail"><br><br><br>
        <input type="submit" value="添加">
    </form>
    <script>
        function uploadPhoto() {
            $("#photoFile").click();
        }
        function upload() {
            var aaa = new FormData();
            aaa.append('photo', document.getElementById('photoFile').files[0]);
            $.ajax({
                url:"${pageContext.request.contextPath}/uploadPhoto",
                type:"post",
                data: aaa,
                contentType: false,
                processData: false,
                success: function(data) {
                    if (data.type == "success") {
                        $("#preview_photo").attr("src", data.filepath+data.filename);
                        $("#productImg").val(data.filename);
                        $("#preview_photo").show();
                    } else {
                        alert(data.msg);
                    }
                },
                error:function(data) {
                    alert("上传失败")
                }
            });
        }
    </script>
</div>